﻿using Service.Domain.Conventions;
using Service.Domain.Conventions.User;
using Service.Domain.Entities.User;
using Service.Domain.Interfaces;
using Service.Domain.Interfaces.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Service.Repository.User
{
    public class RoleRepository : IRoleRepository
    {
        readonly IDbContext context;

        public RoleRepository(IDbContext context)
        {
            this.context = context;
        }

        public void Add(UserRole obj)
        {
            throw new NotImplementedException();
        }

        public IQueryable<UserRole> GetAll(IConvention<UserRole> coneition)
        {
            throw new NotImplementedException();
        }

        public UserRole GetById(int id)
        {
            throw new NotImplementedException();
        }

        public async Task<IEnumerable<UserRole>> GetUserRoles(int userId, ValidRoleConvention condition)
        {
            string sql = @"SELECT R.id, R.`code`, R.`name`, R.`status`
                FROM Roles AS `R`
                JOIN UserRoleRelation AS `URR` ON R.id = URR.RoleId
                JOIN Users AS `U` ON URR.UserId = U.id
                WHERE U.id = @UserId";

            if (condition != null)
            {
                sql += condition.WithSqlPrefix("U").ToSql();
            }

            return await context.QueryAsync<UserRole>(sql, new { UserId = userId });
        }

        public void Remove(int id)
        {
            throw new NotImplementedException();
        }

        public int SaveChanges()
        {
            throw new NotImplementedException();
        }

        public void Update(UserRole obj)
        {
            throw new NotImplementedException();
        }
    }
}
